home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / fill.el < prev    next >
Lisp/Scheme  |  1993-11-12  |  14KB  |  376 lines

  1. ;;; fill.el --- fill commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: wp
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; All the commands for filling text.  These are documented in the Emacs
  26. ;; manual.
  27.  
  28. ;;; Code:
  29.  
  30. (defconst fill-individual-varying-indent nil
  31.   "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
  32. Non-nil means changing indent doesn't end a paragraph.
  33. That mode can handle paragraphs with extra indentation on the first line,
  34. but it requires separator lines between paragraphs.
  35. Nil means that any change in indentation starts a new paragraph.")
  36.  
  37. (defun set-fill-prefix ()
  38.   "Set the fill-prefix to the current line up to point.
  39. Filling expects lines to start with the fill prefix and
  40. reinserts the fill prefix in each resulting line."
  41.   (interactive)
  42.   (setq fill-prefix (buffer-substring
  43.              (save-excursion (beginning-of-line) (point))
  44.              (point)))
  45.   (if (equal fill-prefix "")
  46.       (setq fill-prefix nil))
  47.   (if fill-prefix
  48.       (message "fill-prefix: \"%s\"" fill-prefix)
  49.     (message "fill-prefix cancelled")))
  50.  
  51. (defconst adaptive-fill-mode t
  52.   "*Non-nil means determine a paragraph's fill prefix from its text.")
  53.  
  54. (defconst adaptive-fill-regexp "[ \t]*\\([>*] +\\)?"
  55.   "*Regexp to match text at start of line that constitutes indentation.
  56. If Adaptive Fill mode is enabled, whatever text matches this pattern
  57. on the second line of a paragraph is used as the standard indentation
  58. for the paragraph.")
  59.  
  60. (defun fill-region-as-paragraph (from to &optional justify-flag)
  61.   "Fill region as one paragraph: break lines to fit fill-column.
  62. Prefix arg means justify too.
  63. From program, pass args FROM, TO and JUSTIFY-FLAG."
  64.   (interactive "r\nP")
  65.   ;; Arrange for undoing the fill to restore point.
  66.   (if (and buffer-undo-list (not (eq buffer-undo-list t)))
  67.       (setq buffer-undo-list (cons (point) buffer-undo-list)))
  68.   ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
  69.   (let ((fill-prefix fill-prefix))
  70.     ;; Figure out how this paragraph is indented, if desired.
  71.     (if (and adaptive-fill-mode
  72.          (or (null fill-prefix) (string= fill-prefix "")))
  73.     (save-excursion
  74.       (goto-char (min from to))
  75.       (if (eolp) (forward-line 1))
  76.       (forward-line 1)
  77.       (if (< (point) (max from to))
  78.           (let ((start (point)))
  79.         (re-search-forward adaptive-fill-regexp)
  80.         (setq fill-prefix (buffer-substring start (point))))
  81.         (goto-char (min from to))
  82.         (if (eolp) (forward-line 1))
  83.         ;; If paragraph has only one line, don't assume
  84.         ;; that additional lines would have the same starting
  85.         ;; decoration.  Assume no indentation.
  86. ;;        (re-search-forward adaptive-fill-regexp)
  87. ;;        (setq fill-prefix (make-string (current-column) ?\ ))
  88.         )))
  89.  
  90.     (save-restriction
  91.       (narrow-to-region from to)
  92.       (goto-char (point-min))
  93.       (skip-chars-forward "\n")
  94.       (narrow-to-region (point) (point-max))
  95.       (setq from (point))
  96.       (goto-char (point-max))
  97.       (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
  98.                (regexp-quote fill-prefix))))
  99.     ;; Delete the fill prefix from every line except the first.
  100.     ;; The first line may not even have a fill prefix.
  101.     (and fpre
  102.          (progn
  103.            (if (>= (length fill-prefix) fill-column)
  104.            (error "fill-prefix too long for specified width"))
  105.            (goto-char (point-min))
  106.            (forward-line 1)
  107.            (while (not (eobp))
  108.          (if (looking-at fpre)
  109.              (delete-region (point) (match-end 0)))
  110.          (forward-line 1))
  111.            (goto-char (point-min))
  112.            (and (looking-at fpre) (forward-char (length fill-prefix)))
  113.            (setq from (point)))))
  114.       ;; from is now before the text to fill,
  115.       ;; but after any fill prefix on the first line.
  116.  
  117.       ;; Make sure sentences ending at end of line get an extra space.
  118.       ;; loses on split abbrevs ("Mr.\nSmith")
  119.       (goto-char from)
  120.       (while (re-search-forward "[.?!][])}\"']*$" nil t)
  121.     (insert ? ))
  122.  
  123.       ;; Then change all newlines to spaces.
  124.       (subst-char-in-region from (point-max) ?\n ?\ )
  125.  
  126.       ;; Flush excess spaces, except in the paragraph indentation.
  127.       (goto-char from)
  128.       (skip-chars-forward " \t")
  129.       ;; nuke tabs while we're at it; they get screwed up in a fill
  130.       ;; this is quick, but loses when a sole tab follows the end of a sentence.
  131.       ;; actually, it is difficult to tell that from "Mr.\tSmith".
  132.       ;; blame the typist.
  133.       (subst-char-in-region (point) (point-max) ?\t ?\ )
  134.       (while (re-search-forward "   *" nil t)
  135.     (delete-region
  136.      (+ (match-beginning 0)
  137.         (if (save-excursion
  138.           (skip-chars-backward " ]})\"'")
  139.           (memq (preceding-char) '(?. ?? ?!)))
  140.         2 1))
  141.      (match-end 0)))
  142.       (goto-char (point-max))
  143.       (delete-horizontal-space)
  144.       (insert "  ")
  145.       (goto-char (point-min))
  146.  
  147.       ;; This is the actual filling loop.
  148.       (let ((prefixcol 0) linebeg)
  149.     (while (not (eobp))
  150.       (setq linebeg (point))
  151.       (move-to-column (1+ fill-column))
  152.       (if (eobp)
  153.           nil
  154.         ;; Move back to start of word.
  155.         (skip-chars-backward "^ \n" linebeg)
  156.         ;; Don't break after a period followed by just one space.
  157.         ;; Move back to the previous place to break.
  158.         ;; The reason is that if a period ends up at the end of a line,
  159.         ;; further fills will assume it ends a sentence.
  160.         ;; If we now know it does not end a sentence,
  161.         ;; avoid putting it at the end of the line.
  162.         (while (and (> (point) (+ linebeg 2))
  163.             (eq (preceding-char) ?\ )
  164.             (not (eq (following-char) ?\ ))
  165.             (eq (char-after (- (point) 2)) ?\.))
  166.           (forward-char -2)
  167.           (skip-chars-backward "^ \n" linebeg))
  168.         (if (if (zerop prefixcol)
  169.             (save-excursion
  170.               (skip-chars-backward " " linebeg)
  171.               (bolp))
  172.           (>= prefixcol (current-column)))
  173.         ;; Keep at least one word even if fill prefix exceeds margin.
  174.         ;; This handles all but the first line of the paragraph.
  175.         ;; Meanwhile, don't stop at a period followed by one space.
  176.         (let ((first t))
  177.           (move-to-column prefixcol)
  178.           (while (and (not (eobp))
  179.                   (or first
  180.                   (and (not (bobp))
  181.                        (save-excursion (forward-char -1)
  182.                                (looking-at "\\. ")))))
  183.             (skip-chars-forward " ")
  184.             (skip-chars-forward "^ \n")
  185.             (setq first nil)))
  186.           ;; Normally, move back over the single space between the words.
  187.           (forward-char -1))
  188.         (if (and fill-prefix (zerop prefixcol)
  189.              (< (- (point) (point-min)) (length fill-prefix))
  190.              (string= (buffer-substring (point-min) (point))
  191.                   (substring fill-prefix 0 (- (point) (point-min)))))
  192.         ;; Keep at least one word even if fill prefix exceeds margin.
  193.         ;; This handles the first line of the paragraph.
  194.         ;; Don't stop at a period followed by just one space.
  195.         (let ((first t))
  196.           (while (and (not (eobp))
  197.                   (or first
  198.                   (and (not (bobp))
  199.                        (save-excursion (forward-char -1)
  200.                                (looking-at "\\. ")))))
  201.             (skip-chars-forward " ")
  202.             (skip-chars-forward "^ \n")
  203.             (setq first nil)))))
  204.       ;; Replace all whitespace here with one newline.
  205.       ;; Insert before deleting, so we don't forget which side of
  206.       ;; the whitespace point or markers used to be on.
  207.       (skip-chars-backward " ")
  208.       (insert ?\n)
  209.       (delete-horizontal-space)
  210.       ;; Insert the fill prefix at start of each line.
  211.       ;; Set prefixcol so whitespace in the prefix won't get lost.
  212.       (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
  213.            (progn
  214.          (insert fill-prefix)
  215.          (setq prefixcol (current-column))))
  216.       ;; Justify the line just ended, if desired.
  217.       (and justify-flag (not (eobp))
  218.            (progn
  219.          (forward-line -1)
  220.          (justify-current-line)
  221.          (forward-line 1))))))))
  222.  
  223. (defun fill-paragraph (arg)
  224.   "Fill paragraph at or after point.  Prefix arg means justify as well."
  225.   (interactive "P")
  226.   (let ((before (point)))
  227.     (save-excursion
  228.       (forward-paragraph)
  229.       (or (bolp) (newline 1))
  230.       (let ((end (point))
  231.         (beg (progn (backward-paragraph) (point))))
  232.     (goto-char before)
  233.     (fill-region-as-paragraph beg end arg)))))
  234.  
  235. (defun fill-region (from to &optional justify-flag)
  236.   "Fill each of the paragraphs in the region.
  237. Prefix arg (non-nil third arg, if called from program) means justify as well."
  238.   (interactive "r\nP")
  239.   (save-restriction
  240.    (narrow-to-region from to)
  241.    (goto-char (point-min))
  242.    (while (not (eobp))
  243.      (let ((initial (point))
  244.        (end (progn
  245.          (forward-paragraph 1) (point))))
  246.        (forward-paragraph -1)
  247.        (if (>= (point) initial)
  248.        (fill-region-as-paragraph (point) end justify-flag)
  249.      (goto-char end))))))
  250.  
  251. (defun justify-current-line ()
  252.   "Add spaces to line point is in, so it ends at `fill-column'."
  253.   (interactive)
  254.   (save-excursion
  255.    (save-restriction
  256.     (let (ncols beg indent)
  257.       (beginning-of-line)
  258.       (forward-char (length fill-prefix))
  259.       (skip-chars-forward " \t")
  260.       (setq indent (current-column))
  261.       (setq beg (point))
  262.       (end-of-line)
  263.       (narrow-to-region beg (point))
  264.       (goto-char beg)
  265.       (while (re-search-forward "   *" nil t)
  266.     (delete-region
  267.      (+ (match-beginning 0)
  268.         (if (save-excursion
  269.          (skip-chars-backward " ])\"'")
  270.          (memq (preceding-char) '(?. ?? ?!)))
  271.         2 1))
  272.      (match-end 0)))
  273.       (goto-char beg)
  274.       (while (re-search-forward "[.?!][])\"']*\n" nil t)
  275.     (forward-char -1)
  276.     (insert ? ))
  277.       (goto-char (point-max))
  278.       ;; Note that the buffer bounds start after the indentation,
  279.       ;; so the columns counted by INDENT don't appear in (current-column).
  280.       (setq ncols (- fill-column (current-column) indent))
  281.       (if (search-backward " " nil t)
  282.       (while (> ncols 0)
  283.         (let ((nmove (+ 3 (random 3))))
  284.           (while (> nmove 0)
  285.         (or (search-backward " " nil t)
  286.             (progn
  287.              (goto-char (point-max))
  288.              (search-backward " ")))
  289.         (skip-chars-backward " ")
  290.         (setq nmove (1- nmove))))
  291.         (insert " ")
  292.         (skip-chars-backward " ")
  293.         (setq ncols (1- ncols)))))))
  294.   nil)
  295.  
  296. (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
  297.   "Fill paragraphs within the region, allowing varying indentation within each.
  298. This command divides the region into \"paragraphs\",
  299. only at paragraph-separator lines, then fills each paragraph
  300. using as the fill prefix the smallest indentation of any line
  301. in the paragraph.
  302.  
  303. When calling from a program, pass range to fill as first two arguments.
  304.  
  305. Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
  306. JUSTIFY-FLAG to justify paragraphs (prefix arg),
  307. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  308.   (interactive "r\nP")
  309.   (let ((fill-individual-varying-indent t))
  310.     (fill-individual-paragraphs min max justifyp mailp)))
  311.  
  312. (defun fill-individual-paragraphs (min max &optional justifyp mailp)
  313.   "Fill paragraphs of uniform indentation within the region.
  314. This command divides the region into \"paragraphs\", 
  315. treating every change in indentation level as a paragraph boundary,
  316. then fills each paragraph using its indentation level as the fill prefix.
  317.  
  318. When calling from a program, pass range to fill as first two arguments.
  319.  
  320. Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
  321. JUSTIFY-FLAG to justify paragraphs (prefix arg),
  322. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  323.   (interactive "r\nP")
  324.   (save-restriction
  325.     (save-excursion
  326.       (goto-char min)
  327.       (beginning-of-line)
  328.       (if mailp 
  329.       (while (or (looking-at "[ \t]*[^ \t\n]*:") (looking-at "[ \t]*$"))
  330.         (if (looking-at "[ \t]*[^ \t\n]*:")
  331.         (search-forward "\n\n" nil 'move)
  332.           (forward-line 1))))
  333.       (narrow-to-region (point) max)
  334.       ;; Loop over paragraphs.
  335.       (while (progn (skip-chars-forward " \t\n") (not (eobp)))
  336.     (beginning-of-line)
  337.     (let ((start (point))
  338.           fill-prefix fill-prefix-regexp)
  339.       ;; Find end of paragraph, and compute the smallest fill-prefix
  340.       ;; that fits all the lines in this paragraph.
  341.       (while (progn
  342.            ;; Update the fill-prefix on the first line
  343.            ;; and whenever the prefix good so far is too long.
  344.            (if (not (and fill-prefix
  345.                  (looking-at fill-prefix-regexp)))
  346.                (setq fill-prefix
  347.                  (buffer-substring (point)
  348.                            (save-excursion (skip-chars-forward " \t") (point)))
  349.                  fill-prefix-regexp
  350.                  (regexp-quote fill-prefix)))
  351.            (forward-line 1)
  352.            ;; Now stop the loop if end of paragraph.
  353.            (and (not (eobp))
  354.             (if fill-individual-varying-indent
  355.                 ;; If this line is a separator line, with or
  356.                 ;; without prefix, end the paragraph.
  357.                 (and 
  358.             (not (looking-at paragraph-separate))
  359.             (save-excursion
  360.               (not (and (looking-at fill-prefix-regexp)
  361.                     (progn (forward-char (length fill-prefix))
  362.                         (looking-at paragraph-separate))))))
  363.               ;; If this line has more or less indent
  364.               ;; than the fill prefix wants, end the paragraph.
  365.               (and (looking-at fill-prefix-regexp)
  366.                    (save-excursion
  367.                  (not (progn (forward-char (length fill-prefix))
  368.                          (or (looking-at paragraph-separate)
  369.                          (looking-at paragraph-start))))))))))
  370.       ;; Fill this paragraph, but don't add a newline at the end.
  371.       (let ((had-newline (bolp)))
  372.         (fill-region-as-paragraph start (point) justifyp)
  373.         (or had-newline (delete-char -1))))))))
  374.  
  375. ;;; fill.el ends here
  376.